// pages/daily/index.js
let daily = wx.getStorageSync('daily');
let isFirstCall = true;
const user = wx.getStorageSync('user')
let baseColor = [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
import { getDaily} from "../../utils/request/Api"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    backgroundStyle:{
      background: ""
    },
    month:null,
    day:null,
    dailyList:[]
  },
  setBackgroundGradient () {
    // 获取一个随机的渐变方向
    const directions = [ 'to top'];
    const direction = directions[Math.floor(Math.random() * directions.length)];
    console.log(isFirstCall);
    // 生成两个随机颜色
    function generateSimilarColor(base, variation) {
      return [
        Math.max(0, Math.min(255, base[0] + Math.floor(Math.random() * variation * 2 - variation))),
        Math.max(0, Math.min(255, base[1] + Math.floor(Math.random() * variation * 2 - variation))),
        Math.max(0, Math.min(255, base[2] + Math.floor(Math.random() * variation * 2 - variation)))
      ];
    }
    
    function getRandomColorWithGradientEffect(isFirst) {
      let [r, g, b] = generateSimilarColor(baseColor, 30); // 30 是颜色变化的幅度，可以根据需要调整
      let a = isFirstCall ? (isFirst ? 0 : 0.5) : 1; // 调整alpha值以模拟渐变
      isFirstCall = false;
    
      return `rgba(${r},${g},${b},${a})`;
    }
    const startColor = getRandomColorWithGradientEffect(true);
    const half = getRandomColorWithGradientEffect(false)
    const endColor = getRandomColorWithGradientEffect(false);

    // 动态设置样式
    console.log(`linear-gradient(${direction}, ${startColor} , ${endColor})`)
      this.setData({
        backgroundStyle:{
          background: `linear-gradient(${direction}, ${startColor} 10%,
            ${half} 30%,${half} 50%, ${endColor} 70%,${endColor} 80%,${endColor} 100%)`
        }
      });
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   this.setBackgroundGradient()
   this.getDailySong()
  },
  //获取每日推荐歌曲
  getDailySong(){
    const data={
      user_markings:user.user_markings
    }
    getDaily(data).then(res=>{
      if(res.code==200){
        const {dailyList} = res.data
        this.setData({
          dailyList
        })
      }
    })
  },
  onClickDaily(e){
    const {index,item} = e.currentTarget.dataset
    const {dailyList} = this.data
    wx.setStorageSync('songlist', JSON.stringify(dailyList))
    wx.setStorageSync('musicPlaybackState', {song:JSON.stringify(item)})
    wx.setStorageSync('index', index)
     wx.navigateTo({
       url: '../lyric/index?songlist='+JSON.stringify(dailyList)+"&index="+index,
     })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   if(daily|| (!daily &&daily!=new Date())){    
    wx.setStorageSync('daily', new Date());
   }
     this.setData({
       month:daily.getMonth()+1,
       daily:daily.getDate()
     })
   
   
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})